﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using DataContracts;
using TrainingWorkshop.MVPWebsite.Role;
using System.ServiceModel;
using System.Data.SqlClient;

namespace TrainingWorkshop.MVPWebsite.Role.Views
{
    public class RoleDetailPresenter : Presenter<IRoleDetailView>
    {

        private IRoleController _controller;

        public RoleDetailPresenter([CreateNew] IRoleController controller)
        {
            _controller = controller;
        }

        public void DisplayRoleDetail()
        {
            View.DisplayAllAuthority = _controller.GetAllAuthority();
            if (View.GetTransRoleId != 0 && _controller.GetRoleById(View.GetTransRoleId) != null)
            {
                DataContracts.RoleDTO roleDTO = _controller.GetRoleById(View.GetTransRoleId);
                View.roleDTO = roleDTO;
                View.SelectedStatus = roleDTO.Status;
                View.DisplayRoleName = roleDTO.RoleName;
                View.SelectedAuthority = roleDTO.Authorities;

            }
        }

        public void AddOrUpdateRoleDetail()
        {
          
            if (View.GetCheckboxlistCount == 0)
            {
                View.ShowOperationMessage("Please add role authority first");
                return;
            }
            try
            {
                if (View.GetTransRoleId == 0)
                {
                    DataContracts.RoleDTO roleDTO = new RoleDTO();
                    View.roleDTO = roleDTO;
                    roleDTO.RoleName = View.GetRoleName;
                    roleDTO.Status = View.SelectStatus;
                    roleDTO.Authorities = View.UpdateRoleAuthority;
                    _controller.AddNewRole(roleDTO);
                }

                if (View.GetTransRoleId > 0)
                {
                    RoleDTO roleDTO = new RoleDTO();
                    View.roleDTO = roleDTO;
                    roleDTO.RoleId = View.GetTransRoleId;
                    roleDTO.RoleName = View.GetRoleName;
                    roleDTO.Status = View.SelectStatus;
                    roleDTO.Authorities = View.AddRoleAuthority;
                    _controller.UpdateRole(roleDTO);

                }
               
                View.Redirect("BatchUpdateRoleStatus.aspx");
            }
            catch (FaultException ex)
            {
                
                View.ShowOperationMessage(ex.Message);

                if (ex.Code.Name == "Role Deleted")
                {
                    View.Redirect("BatchUpdateRoleStatus.aspx");
                }
                else  
                {
                    View.DisplayAllAuthority = _controller.GetAllAuthority();
                }
            }
        }



        public override void OnViewLoaded()
        {
            
            DisplayRoleDetail();
        }

        public override void OnViewInitialized()
        {
           
        }

    }
}




